<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <link rel="stylesheet" href="./font/iconfont.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        header {
            display: flex;
            justify-content: space-between;
            padding: 20px;
            background-color: rgb(27, 101, 174);
            color: white;
        }

        .content {
            display: flex;
        }

        .content .left {
            list-style: none;
            width: 150px;
            background-color: rgb(27, 101, 174);
            color: rgb(201, 197, 197);
        }

        .content .left li {
            text-align: center;
            line-height: 40px;
            padding: 0 20px;
        }

        .content .left li.active {
            background-color: rgb(42, 79, 147);
            color: white;
        }

        .content .right {
            flex: 1;
            padding: 30px;
        }

        .content .right .input-box {
            margin: 20px 0;
        }

        .content .right .input-box input {
            width: 300px;
            height: 40px;
            border-radius: 20px;
            padding-left: 20px;
            outline: none;
            border: 1px solid gray;
            margin-right: 20px;
        }

        .content .right .input-box button,
        .add {
            width: 100px;
            height: 40px;
            border: 0;
            border-radius: 5px;
            background-color: rgb(113, 174, 22);
            color: white;
            cursor: pointer;
        }

        .content .right table {
            width: 80%;
            text-align: center;
            line-height: 40px;
        }

        .content .right table tr th:first-child,
        .content .right table tbody td:first-child {
            text-align: left;
        }

        .content .right table tbody td:nth-child(6) {
            width: 50px;
            background-color: rgb(113, 174, 22);
            color: white;
            border-radius: 10px;
        }

        .content .right table tbody td:nth-child(6).error {
            background-color: red;
        }

        .content .right .foot {
            text-align: center;
            line-height: 40px;
        }

        .content .right .foot button {
            border: 1px solid gray;
            padding: 0 20px;
            display: inline-block;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <header>
        <span>9466云专题</span>
        <span class="iconfont icon-yonghu"> <span class="usersname"></span> （9466云专题）</span>
    </header>
    <div class="content">
        <ul class="left">
            <li class="iconfont icon-home"> 首页</li>
            <li class="iconfont icon-zhuantiguanli"> 专题</li>
            <li class="iconfont icon-yonhu active"> 用户</li>
            <li class="iconfont icon-shezhi"> 设置</li>
            <li class="iconfont icon-xitongrizhi"> 日志</li>
        </ul>
        <div class="right">
            <div class="input-box">
                <input type="text" placeholder="搜索用户">
                <button>搜索</button>
            </div>
            <button class="iconfont icon-tianjia add"> 添加用户</button>
            <table>
                <thead>
                    <tr>
                        <th>邮箱(账户)</th>
                        <th>姓名</th>
                        <th>电话</th>
                        <th>所属组</th>
                        <th>管理员</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr>
                        <td>1@sss.com</td>
                        <td>丽丽</td>
                        <td>237235271</td>
                        <td>1</td>
                        <td>是</td>
                        <td class="state">正常</td>
                        <td>
                            <span class="iconfont icon-bianji"></span>
                            <span class="iconfont icon-shanchu"></span>
                        </td>
                    </tr> -->
                </tbody>
            </table>
            <div class="foot">
                <button class="pre">上一页</button>
                <button class="next">下一页</button>
            </div>
        </div>
    </div>
</body>

</html>
<script src="./axios.min.js"></script>
<script>
    var input = document.querySelector('.input-box input')
    var searchbtn = document.querySelector('.input-box button')

    //获取localStorage中存储的用户信息
    var userinfo = JSON.parse(localStorage.getItem('userinfo'))
    document.querySelector('.usersname').innerHTML=userinfo.name

    //点击搜索
    searchbtn.onclick = function () {
        axios.get('/user/search', { params: { key: input.value } }).then((res) => {
            render(res.data.list)
        })
    }

    var tbody = document.querySelector('tbody')
    tbody.onclick = (e) => {
        if (e.target.classList.contains('icon-shanchu')) {
            axios.post('/user/delete', { phone: e.target.dataset.phone }).then((res) => {
                render(res.data.list)
            })
        }
    }

    //渲染列表
    function render(data) {
        var str = data.map((item) => {
            return `
                    <tr>
                        <td>${item.email}</td>
                        <td>${item.name}</td>
                        <td>${item.phone}</td>
                        <td>${item.ingroup}</td>
                        <td>${item.isadmin ? '是' : '否'}</td>
                        <td class="${item.state ? '' : 'error'}">${item.state ? '正常' : '异常'}</td>
                        <td>
                            <span class="iconfont icon-bianji"></span>
                            <span class="iconfont icon-shanchu" data-phone="${item.phone}"></span>
                        </td>
                    </tr>
                    `
        }).join('')
        if (str == '') {
            str = '<tr><td style="text-align:center;color:red">未查询到数据!</td></tr>'
        }
        document.querySelector('tbody').innerHTML = str
    }

    var pre = document.querySelector('.foot .pre')
    var next = document.querySelector('.foot .next')
    var page = 1
    axios.get('/user/list', { params: { page: page, count: 3 } }).then((res) => {
        render(res.data.list);
    })
    //下一页
    next.onclick = () => {
        pre.disabled = false
        axios.get('/user/list', { params: { page: ++page, count: 3 } }).then((res) => {
            render(res.data.list);
        })
        if (page == 3) {
            next.disabled = true
        }
    }
    //上一页
    pre.onclick = () => {
        next.disabled = false
        axios.get('/user/list', { params: { page: --page, count: 3 } }).then((res) => {
            render(res.data.list);
        })
        if (page == 1) {
            pre.disabled = true
        }
    }
</script>